package redis

import (
	"context"
	"fmt"
	"gitee.com/wuzheng0709/backend-gopkg/infrastructure/config"
	"gitee.com/wuzheng0709/backend-gopkg/infrastructure/pkg/gin/log"
	"github.com/go-redis/redis"
	"time"
)

var ctx = context.Background()
var ImRedisDB *redis.Client
var AliyunRedisDB *redis.Client

func init() {
	ImRedisDB = GetRedisClientPool(config.C.Redis.Db)
	AliyunRedisDB = GetAliyunRedisClientPool(config.C.System.AliyunRedis.Db)
}
func GetRedisClientPool(db int) *redis.Client {
	log.Info("redis  链接中。。。")
	redisdb := redis.NewClient(&redis.Options{
		Addr:     config.C.Redis.Host + ":" + config.C.Redis.Port,
		Password: config.C.Redis.Auth,
		DB:       db,
		PoolSize: 5})
	pong, err := redisdb.Ping().Result()
	if err != nil {
		fmt.Println(pong, err)
	}
	log.Info("redis  链接成功。。。")
	return redisdb
}

func GetAliyunRedisClientPool(db int) *redis.Client {
	log.Info("redis  链接中。。。")
	redisdb := redis.NewClient(&redis.Options{
		Addr:     config.C.System.AliyunRedis.Host + ":" + config.C.System.AliyunRedis.Port,
		Password: config.C.System.AliyunRedis.Auth,
		DB:       db,
		PoolSize: 5})
	pong, err := redisdb.Ping().Result()
	if err != nil {
		fmt.Println(pong, err)
	}
	log.Info("redis  链接成功。。。")
	return redisdb
}

// 向key的hash中添加元素field的值
func HashSet(client *redis.Client, key, field string, data interface{}) {

	err := client.HSet(key, field, data)
	if err != nil {
		log.Info("Redis HSet Error:", err)
	}
	return
}

// 批量向key的hash添加对应元素field的值
func BatchHashSet(client *redis.Client, key string, fields map[string]interface{}, duration time.Duration) (string, error) {
	val, err := client.HMSet(key, fields).Result()
	client.Expire(key, duration) //config.C.Jwt.TimeOut*time.Hour
	if err != nil {
		log.Info("Redis HMSet Error:", err)
		return val, err
	}
	return val, err
}

// 通过key获取hash的元素值
func HashGet(client *redis.Client, key, field string) string {
	result := ""
	val, err := client.HGet(key, field).Result()
	if err == redis.Nil {
		log.Info("Key Doesn't Exists:", field)
		return result
	} else if err != nil {
		log.Info("Redis HGet Error:", err)
		return result
	}
	return val
}
func HashGetAll(client *redis.Client, key string) (map[string]string, error) {
	return client.HGetAll(key).Result()
}

// 通过steamXAdd
func XAdd(client *redis.Client, stream string, MaxLen, MaxLenApprox int64, values map[string]interface{}) string {
	var result redis.XAddArgs
	result.Stream = stream
	result.MaxLen = MaxLen
	//result.MaxLenApprox = MaxLenApprox
	result.Values = values
	val, err := client.XAdd(&result).Result()
	log.Info("Redis 消息", val, err)
	if err != nil {
		log.Info("Redis XAdd Error:", err)
	}
	return val
}

// 批量获取key的hash中对应多元素值
func BatchHashGet(client *redis.Client, key string, fields ...string) map[string]interface{} {
	resMap := make(map[string]interface{})
	for _, field := range fields {
		var result interface{}
		val, err := client.HGet(key, fmt.Sprintf("%s", field)).Result()
		if err == redis.Nil {
			log.Info("Key Doesn't Exists:", field)
			resMap[field] = result
		} else if err != nil {
			log.Info("Redis HMGet Error:", err)
			resMap[field] = result
		}
		if val != "" {
			resMap[field] = val
		} else {
			resMap[field] = result
		}
	}
	return resMap
}

// 获取自增唯一ID
func Incr(client *redis.Client, key string) int {
	val, err := client.Incr(key).Result()
	if err != nil {
		log.Info("Redis Incr Error:", err)
	}
	return int(val)
}

// 添加集合数据
func SetAdd(client *redis.Client, key, val string) {
	client.SAdd(key, val)
}

// 从集合中获取数据
func SetGet(client *redis.Client, key string) []string {
	val, err := client.SMembers(key).Result()
	if err != nil {
		log.Info("Redis SMembers Error:", err)
	}
	return val
}
